<?php
namespace Admin\Model;
//管理员模型
class AdminModel extends CommonModel
{
    //定义字段
    protected $fields = array('id', 'username', 'password');
    //自动验证
    protected $_validate = array(
        array('username', 'require', '用户名必须填写！'),
        array('username', '', '用户名重复！'),
        array('password', 'require', '密码必须填写'),
    );
    //自动完成
    protected $_auto = array(
        array('password', 'md5', 3, 'function')
    );


    //使用TP的钩子函数实现，中间表的入库----钩子方法，在调用这个对象add数据的时候自动执行
    protected function _after_insert($data)
    {
        $admin_role = array(
            'admin_id' => $data['id'],
            'role_id' => I('post.role_id')
        );
        M('AdminRole')->add($admin_role);
    }

    //获取数据显示
    public function listData()
    {
        //定义页尺寸
        $pagesize = 10;
        //计算数据总数
        $count = $this->count();

        //生成分页导航信息
        $page = new \Think\Page($count, $pagesize);
        $show = $page->show();
        //接收当前所在的页码
        $p = intval(I('get.p'));
        $list = $this->alias('a')->field('a.*,b.role_id,c.role_name')
            ->join("left join agency_admin_role b on a.id=b.admin_id")->join("agency_role c on b.role_id=c.id")->page($p, $pagesize)->select();
        //dump($list);die;
        return array('pageStr' => $show, 'list' => $list);
    }

    //添加管理员
    public function create()
    {
        $data = I('post.');

        $map['username'] = $data['username'];
        $map['password'] = md5($data['password']);
        $admin_id = $this->add($map);
        if ($admin) {
            $maps['role_id'] = $data['role_id'];
            $maps['admin_id'] = $admin_id;
            $rel = M('admin_role')->add($maps);
            if ($rel) {
                return 1;
            }
        }

    }

    //删除管理员
    public function remove($admin_id)
    {
        //开启事务
        $this->startTrans();
        //1、删除对应的用户信息  jx_admin
        $userStatus = $this->where("id=$admin_id")->delete();
        if (!$userStatus) {
            $this->rollback();
            return false;
        }
        //2、删除对应的角色信息  jx_admin_role
        $roleStatus = D('AdminRole')->where("admin_id=$admin_id")->delete();
        if (!$roleStatus) {
            $this->rollback();
            return false;
        }

        $this->commit();
        return true;
    }

    //查询    //根据用户的ID获取用户的信息及角色的ID
    public function findOne($admin_id)
    {
        return $this->alias('a')->field("a.*,b.role_id")->join('left join agency_admin_role b on a.id=b.admin_id')->where("a.id=$admin_id")->find();
    }

    //修改
    public function update($data)
    {
        $role_id = intval(I("post.role_id"));
        //修改用户的基本信息
        $this->save($data);
        //修改用户对应的角色
        M('AdminRole')->where('admin_id=' . $data['id'])->save(array('role_id' => $role_id));
    }

    public function login($username, $password)
    {
        //1、根据用户名查询用户信息
        $userinfo = $this->where(array("username" => $username))->find();
        if (!$userinfo) {
            $this->error = "用户名不存在";
            return false;
        }
        //2、根据密码进行比对
        if ($userinfo['password'] != md5($password)) {
            $this->error = "密码错误";
            return false;
        }
        //说明该用户信息是正确的可以登录
        //保存用户的登录状态
        cookie('admin', $userinfo);
        session('username', $username);
        session('userid', $userinfo['id']);
        return true;
    }

}